#!/usr/bin/make -f

DISTRIBUTION = $(shell lsb_release -sr)
VERSION = 4.4.7
PACKAGEVERSION = $(VERSION)-1~$(DISTRIBUTION)0
TARBALL = apache-karaf-$(VERSION)-src.tar.gz
URL = http://apache.uib.no/karaf/$(VERSION)/$(TARBALL)
SRC=unpacked-src
KARAF_ASSEMBLY=$(SRC)/assemblies/apache-karaf/target/assembly
DESTDIR=debian/karaf

%:
	dh $@ --with systemd

override_dh_auto_clean:
	rm -rf $(SRC) $(DESTDIR)
override_dh_auto_test:
override_dh_auto_build:
	wget -N --progress=dot:mega $(URL)
	mkdir -p $(SRC)
	(cd $(SRC); tar --strip-components=1 -xf ../$(TARBALL) )
	sed -i -e 's#<module>demos</module>##' -e 's#<module>itests</module>##' $(SRC)/pom.xml
	(cd $(SRC); mvn clean install -DskipTests=true )
override_dh_auto_install:
	mkdir -p $(DESTDIR)/etc/karaf
	mkdir -p $(DESTDIR)/usr/share/karaf
	mkdir -p $(DESTDIR)/usr/share/doc/karaf
	mkdir -p $(DESTDIR)/var/lib/karaf
	mv $(KARAF_ASSEMBLY)/bin $(DESTDIR)/usr/share/karaf
	mv $(KARAF_ASSEMBLY)/data $(DESTDIR)/var/lib/karaf
	mv $(KARAF_ASSEMBLY)/deploy $(DESTDIR)/var/lib/karaf
	mv $(KARAF_ASSEMBLY)/etc/* $(DESTDIR)/etc/karaf
	mv $(KARAF_ASSEMBLY)/lib $(DESTDIR)/usr/share/karaf
	mv $(KARAF_ASSEMBLY)/system $(DESTDIR)/usr/share/karaf
	(cd files; tar cf - etc) | (cd $(DESTDIR); tar xf -)
	sed -i -e '/system.repository@snapshots, \\/ a \ \ \ \ file:\/usr\/share\/maven-repo@id=debian.repository, \\' $(DESTDIR)/etc/karaf/org.ops4j.pax.url.mvn.cfg
	sed -i -e 's#jansi/2.4.1#jansi/debian#' $(DESTDIR)/etc/karaf/startup.properties
	sed -i -e 's#\$${karaf.log}#/var/log/karaf#' $(DESTDIR)/etc/karaf/org.ops4j.pax.logging.cfg
	sed -i -e 's#log4j2.appender.rolling.policies.size.size = .*#log4j2.appender.rolling.policies.size.size = 1M#' $(DESTDIR)/etc/karaf/org.ops4j.pax.logging.cfg
	rm $(DESTDIR)/usr/share/karaf/bin/*.bat
	rm -rf $(DESTDIR)/usr/share/karaf/bin/contrib
	sed -i -e 's#KARAF_BASE}/etc#KARAF_ETC}#' $(DESTDIR)/usr/share/karaf/bin/karaf
	(cd $(DESTDIR)/var/lib/karaf/; ln -s /usr/share/karaf/bin/ .)
	(cd $(DESTDIR)/var/lib/karaf/; ln -s /usr/share/karaf/lib/ .)
	(cd $(DESTDIR)/var/lib/karaf/; ln -s /usr/share/karaf/system/ .)
	rm -rf $(DESTDIR)/usr/share/karaf/system/org/apache/karaf/demos
	rm -rf $(DESTDIR)/usr/share/karaf/system/org/slf4j/
	rm -rf $(DESTDIR)/usr/share/karaf/system/net/
	rm -rf $(DESTDIR)/usr/share/karaf/system/org/ow2/
	rm -rf $(DESTDIR)/usr/share/karaf/system/org/eclipse/
	rm -rf $(DESTDIR)/usr/share/karaf/system/org/apache/aries/
	rm -rf $(DESTDIR)/usr/share/karaf/system/org/apache/sshd/
	rm -rf $(DESTDIR)/usr/share/karaf/system/org/apache/felix/org.apache.felix.webconsole.plugins.event/
	rm -rf $(DESTDIR)/usr/share/karaf/system/org/ops4j/pax/logging/pax-logging-logback/
	rm -rf $(DESTDIR)/usr/share/karaf/system/org/ops4j/pax/url/pax-url-wrap/
	rm -rf $(DESTDIR)/usr/share/karaf/system/org/jline/
	rm -rf $(DESTDIR)/usr/share/karaf/system/org/fusesource/jansi/jansi/
	(cd $(DESTDIR)/usr/share/karaf/lib/boot/; ln -s /usr/share/maven-repo/net/java/dev/jna/jna/debian/jna-debian.jar jna.jar)
	(cd $(DESTDIR)/usr/share/karaf/lib/boot/; ln -s /usr/share/maven-repo/net/java/dev/jna/jna-platform/debian/jna-platform-debian.jar jna-platform.jar)
	mkdir -p $(DESTDIR)/usr/share/karaf/system/org/fusesource/jansi/jansi/
	(cd $(DESTDIR)/usr/share/karaf/system/org/fusesource/jansi/jansi/; ln -s /usr/share/maven-repo/org/fusesource/jansi/jansi/debian/ debian)

override_dh_gencontrol:
	dh_gencontrol -- -v$(PACKAGEVERSION)
